import time
from faker import Faker
faker = Faker(locale='zh_cn')
phone = faker.phone_number()
case = """
1、点击我的
2、点击立即借款
3、输入手机号
4、勾选阅读并同意
5、点击获取短信验证码
6、验证短信发送的手机号是否正确
预期结果：
6、输入的手机号和发送的手机号一致
"""
from appium import webdriver
from appium.webdriver.common.mobileby import MobileBy
from appium.webdriver.common.touch_action import TouchAction
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
caps = {
  "platformName": "Android",
  "platformVersion": "7.1.2",
  "appPackage": "com.lingyue.zebraloan",
  "appActivity": "com.lingyue.banana.activities.BananaLaunchActivity",
  "deviceName": "x1q"
}
driver = webdriver.Remote("http://localhost:4723/wd/hub",caps)
driver.implicitly_wait(15)

# 点击 同意按钮
driver.find_element(MobileBy.ID,"com.lingyue.zebraloan:id/tv_confirm").click()

# 点击我的：ID定位
WebDriverWait(driver,10,0.5).until(EC.presence_of_all_elements_located((MobileBy.ID,"com.lingyue.zebraloan:id/iv_icon")))[1].click()
# time.sleep(5)
# driver.find_elements(MobileBy.ID,"com.lingyue.zebraloan:id/iv_icon")[1].click()

# 点击立即借款
driver.find_element(MobileBy.ID,"com.lingyue.zebraloan:id/btn_loan").click()

# 输入手机号码
driver.find_element(MobileBy.ID,"com.lingyue.zebraloan:id/et_mobile_number").send_keys(str(phone))

# 点击勾选隐私政策
time.sleep(3)
TouchAction(driver).tap(x = 48,y = 860).perform()

# 点击获取验证码
driver.find_element(MobileBy.ID,"com.lingyue.zebraloan:id/btn_next").click()

"""# 断言是否跳到验证码页面
phone_code = driver.find_elements(MobileBy.ID,"com.lingyue.zebraloan:id/tv_title")[1].text
assert phone_code == "短信验证码"

# 断言手机号是否正确
phone_number = driver.find_element(MobileBy.ID,"com.lingyue.zebraloan:id/tv_phone").text
assert str(phone)[:4] in phone_number
assert str(phone)[-1:-4] in phone_number"""
